;(function(){

  const CompInfo = { name: 'AnimeBox', version: 'v2' };
  
  main();

  function main() {
    getYepNope(() => {
      yepnope({
        test: window.axlib,
        nope: ['https://ax.minicg.com/axlib-v3.min.js', 'https://ax.minicg.com/anime.min.js'],
        complete: init
      });
    });
  }

  function init() {
    let repScripts = $('script[type="axure-repeater-template"]');
    $.each(repScripts, (i, k) => {
      let rep = $(k).parent();
      let repId = rep.attr('id');
      let repData = $axure(`#${repId}`).getRepeaterData({format:'row',first:true});

      if ( matchComponent($axure(`#${repId}`).getRepeaterData({format:'row',first:false}), CompInfo.name, CompInfo.version) ) {
        let moveMode = repData['位移模式'].toLowerCase() == 'relative' ? 'relative' : 'absolute';
        let moveOrigin = moveMode === 'relative' ? { x: 0, y: 0 } : { x: Math.floor(rep.position().left), y: Math.floor(rep.position().top) };

        let position = formatVec2(repData['位移'], moveOrigin) || [0, 0];
        let rotation = formatVec1(repData['旋转']);
        let scale = formatVec1(repData['缩放']);
        let opacity = formatVec1(repData['透明度']);
        let duration = Number(repData['动画时长']);
        let firstDelay = Number(repData['初始延时']);
        let delay = Number(repData['启动延时']);
        let endDelay = Number(repData['结束延时']);
        let direction = repData['动画方向'] || 'normal';
        let easing = repData['缓动曲线'] || 'easeOutCubic';
        let loop = Number(repData['循环次数']);
        if (loop == -1) { loop = true; }
        let cursorStyle = repData['鼠标样式'] || 'default';
        let pointerEvents = repData['鼠标穿透'].toLowerCase() == 'true' ? 'none' : 'auto';
        let filters = repData['滤镜效果'] || 'none';
        let mixBlendMode = repData['混合模式'] || 'normal';
        let initOpacity = repData['初始隐藏'].toLowerCase() == 'true' ? 0 : 1;

        let animeOpts = {
          targets: '#' + repId,
          translateX: position.x,
          translateY: position.y,
          rotate: rotation,
          scale,
          opacity,
          easing,
          direction,
          loop,
          duration,
          delay,
          endDelay
        };

        rep.css({
          'cursor': cursorStyle,
          'pointer-events': pointerEvents,
          'filter': filters,
          'mix-blend-mode': mixBlendMode,
          'opacity': initOpacity
        });

        // if (initOpacity === 0 && opacity.length > 1 && opacity[0].value === 0) { opacity = opacity.shift(); }

        setTimeout(()=>{ anime(animeOpts); }, firstDelay);

      } else {
        axlib.use(['toast'], plugins => {
          let { toast } = plugins;
          console.log('AnimeBox组件版本不匹配!');
          toast({
            heading: '数据错误',
            text: 'AnimeBox组件版本不匹配',
            showHideTransition: 'slide',
            icon: 'error'
          });
        });
      }

    });
  }

  function formatVec1(str) {
    let arrValue = [];
    str = str.replace(/ /g, '');
    let list = str.split(',');
    arrValue = list.map(item => { return { value: item } });
    return arrValue;
  }

  function formatVec2(str, origin) {
    let arrX = [], arrY = [];
    if (str.split('[').length == 2) {
      // 单个坐标
      str = str.replace(' ', '').replace('[', '').replace(']', '');
      let pos = str.split(',');
      arrX = [{ value: Number(pos[0]) - origin.x }];
      arrY = [{ value: Number(pos[1]) - origin.y }];
    } else {
      // 多个坐标
      str = str.replace(/ /g, '').replace(/\]\,\[/g, '_').replace(/\[/g, '').replace(/\]/, '');
      let list = str.split('_');
      list.forEach(item => {
        let pos = item.split(',');
        arrX.push({ value: Number(pos[0]) - origin.x });
        arrY.push({ value: Number(pos[1]) - origin.y });
      });
    }
    return { x: arrX, y: arrY };
  }
  
  function matchComponent(data, name, version) {
    let keyName = '组件信息';
    if (data[keyName] && data[keyName][0]==name && data[keyName][1].indexOf(version) != -1) {
      return true;
    }
    return false;
  }

  function getYepNope(callback) {
    if (window.yepnope) {
      callback();
    } else {
      $.getScript('https://ax.minicg.com/axloader.min.js', callback);
    }
  }
}());